﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL
{
    public class GiftCardDAL : DBController
    {
        public GiftCardDAL()
        { }

        public List<MontoGiftCardEntity> GetMontosGiftCard()
        {
            List<MontoGiftCardEntity> montos = new List<MontoGiftCardEntity>();

            using (SqlConnection conn = this.OpenConn())
            {
                SqlDataReader reader;
                SqlCommand command = new SqlCommand("maravilla_sql.spGetMontosGiftCard", conn);

                command.CommandType = CommandType.StoredProcedure;

                reader = command.ExecuteReader();
                command.Dispose();

                while (reader.Read())
                {
                    //Se cargan los montos como floats
                    //float aux;
                    //double monto = Convert.ToDouble(reader["monto"]);
                    //aux = (float)monto;
                    //montos.Add(aux);

                    int i;
                    MontoGiftCardEntity monto = new MontoGiftCardEntity();

                    i = reader.GetOrdinal("intIdMonto");
                    if (!reader.IsDBNull(i))
                        monto.IdMonto = reader.GetInt32(i);

                    i = reader.GetOrdinal("monto");
                    if (!reader.IsDBNull(i))
                        monto.Saldo = (float)reader.GetDecimal(i);

                    montos.Add(monto);
                }
            }

            return montos;
        }

        public void ConfirmGiftCard(int IdClienteOrigen, int idClienteDestino, int idMonto, string fechaActual)
        {
            using (SqlConnection conn = this.OpenConn())
            {
                SqlCommand command = new SqlCommand("maravilla_sql.spConfirmGiftCard", conn);

                command.CommandType = CommandType.StoredProcedure;
                //Parametros de entrada
                command.Parameters.AddWithValue("@idclienteorigen", IdClienteOrigen);
                command.Parameters.AddWithValue("@idclientedestino", idClienteDestino);
                command.Parameters.AddWithValue("@idMonto", idMonto);

                SqlParameter param = new SqlParameter("@fecha", fechaActual);
                param.SqlDbType = SqlDbType.Char;
                param.Size = 10;
                param.Direction = ParameterDirection.Input;

                command.Parameters.Add(param);

                command.ExecuteNonQuery();
                command.Dispose();
            }
        }
    }
}
